﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;

public partial class Admin_UserControls_MenuVertical : System.Web.UI.UserControl
{
    protected DataTable dtGetByAdmin;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Repeater1_PreRender(object sender, EventArgs e)
    {
        int AdminID = 1; // phải bắt từ session
        DataTable dt = AdminRole.GetByAdminID(AdminID);
        dtGetByAdmin = dt;
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex <= 0)
        {
            return;
        }
        DataRowView row = (DataRowView)e.Item.DataItem;
        Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
        Role role = Role.GetInfo(Convert.ToInt32(row["role_id"].ToString()));
        int RootID = Role.GetInfoByParentID(0).Role_id;
        int ParentID = role.Parent_id;
        if (ParentID == RootID)
        {
            Literal1.Text = "<h6 id=\"h-menu-" + role.Role_id + "\"><a href=\"#"
                + role.Role_id + "\"><span>" + role.Role_name + "</span></a></h6>";

            Literal1.Text += "<ul id=\"menu-" + role.Role_id + "\" class=\"closed\">";
            DataTable dt = Role.GetAllByParentID(role.Role_id);
            foreach (DataRow r in dt.Rows)
            {
                if (CheckExist(dtGetByAdmin, "role_id", Convert.ToInt32(r["role_id"].ToString())))
                {
                    Literal1.Text += "<li><a href=\"" + r["path"].ToString()
                        + "\">" + r["role_name"].ToString() + "</a></li>";
                }
            }
            Literal1.Text += "</ul>";
        }
    }

    protected bool CheckExist(DataTable dt, string col, int id)
    {
        foreach (DataRow row in dt.Rows)
        {
            int rid = Convert.ToInt32(row[col].ToString());
            if (rid == id)
                return true;
        }
        return false;
    }
}
